package com.sxu.douban.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sxu.douban.common.HigherResponse;
import com.sxu.douban.entity.Movie;
import com.sxu.douban.entity.Rating;
import com.sxu.douban.entity.Recommend;
import com.sxu.douban.entity.User;
import com.sxu.douban.mapper.MovieDao;
import com.sxu.douban.mapper.RatingDao;
import com.sxu.douban.mapper.RecommendDao;
import com.sxu.douban.mapper.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.List;

@RestController
public class UserCon {

    @Autowired
    private UserDao userDao;

    @Autowired
    private MovieDao movieDao;

    @Autowired
    private RatingDao ratingDao;

    @Autowired
    private RecommendDao recommendDao;

    @RequestMapping("/login.do")
    public HigherResponse<User> queryUser(@RequestParam(value = "userId",required = false)String userId,
                                          @RequestParam(value = "password",required = false)String password,
                                          HttpSession session){
        if (null == userId || null == password){
            return HigherResponse.getFailedRespon("用户名密码不能为空");
        }
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("u_id",userId);
        wrapper.eq("u_passwd",password);
        User user = userDao.selectOne(wrapper);
        if (null == user){
            return HigherResponse.getFailedRespon("用户名或密码错误");
        }
        session.setAttribute("userInfo",user);
        return HigherResponse.getSuccessRespon(user);
    }

    @RequestMapping("/register.do")
    public HigherResponse<String> addOneUser(@RequestParam(value = "userId",required = false)String userId,
                                             @RequestParam(value = "password",required = false)String password){
        if (null == userId || null == password){
            return HigherResponse.getFailedRespon("用户名密码不能为空");
        }

        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("u_id",userId);
        wrapper.eq("u_passwd",password);
        User user = userDao.selectOne(wrapper);
        if (null != user){
            return HigherResponse.getFailedRespon("用户名已经存在");
        }
        User user1 = new User();
        user1.setUserId(userId);
        user1.setPassword(password);

        int insert = userDao.insert(user1);

        if (insert == 0){
            return HigherResponse.getFailedRespon("注册失败");
        }

        return HigherResponse.getSuccessRespon("注册成功");
    }

    //电影首页-电影按照评分高低排序展示
    @RequestMapping("listMovies.do")
    public HigherResponse<PageInfo<Movie>> listMovies(@RequestParam(required = true,defaultValue = "1") Integer pageNum,
                                                      @RequestParam(required = true,defaultValue = "5") Integer pageSize){
        PageHelper.startPage(pageNum,pageSize);
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.orderByDesc("rate");
        List<Movie> movies = movieDao.selectList(wrapper);
        PageInfo<Movie> moviePageInfo = new PageInfo<>(movies);
        return HigherResponse.getSuccessRespon(moviePageInfo);
    }

    //根据电影名称查询
    @RequestMapping("/getMovieByName.do")
    public HigherResponse<PageInfo<Movie>> getMovieByName(@RequestParam(required = true,defaultValue = "1") Integer pageNum,
                                                          @RequestParam(required = true,defaultValue = "5") Integer pageSize,
                                                          HttpSession session, String movieName){
        Object attribute = session.getAttribute("userInfo");
        User user = null;
        if (attribute instanceof User){
            user = (User) attribute;
        }
        if (null == user){
            return HigherResponse.getFailedRespon("未登录，请重新登录");
        }
        PageHelper.startPage(pageNum,pageSize);
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.like("m_name",movieName);
        List<Movie> movieList = movieDao.selectList(wrapper);
        PageInfo<Movie> moviePageInfo = new PageInfo<>(movieList);
        return HigherResponse.getSuccessRespon(moviePageInfo);
    }

    //根据电影id查询一个电影
    @RequestMapping("/getMovieById.do")
    public HigherResponse<Movie> getMovieById(HttpSession session, String movieId){
        Object attribute = session.getAttribute("userInfo");
        User user = null;
        if (attribute instanceof User){
            user = (User) attribute;
        }
        if (null == user){
            return HigherResponse.getFailedRespon("未登录，请重新登录");
        }
        Movie movie = movieDao.selectById(movieId);
        return HigherResponse.getSuccessRespon(movie);
    }

    //根据电影id  查询到多个推荐电影的id 然后批量显示推荐电影列表
    @RequestMapping("/getMoviesByIds.do")
    public HigherResponse<PageInfo<Movie>> getMoviesByIds(HttpSession session, String movieId,
                                                          @RequestParam(required = true,defaultValue = "1") Integer pageNum,
                                                          @RequestParam(required = true,defaultValue = "5") Integer pageSize){
        Object attribute = session.getAttribute("userInfo");
        User user = null;
        if (attribute instanceof User){
            user = (User) attribute;
        }
        if (null == user){
            return HigherResponse.getFailedRespon("未登录，请重新登录");
        }
        PageHelper.startPage(pageNum,pageSize);
        Recommend recommend = recommendDao.selectById(movieId);
        String movieIds = recommend.getRecommendMovieId();
        String[] split = movieIds.split(",");
        List<Movie> movieList = movieDao.selectBatchIds(Arrays.asList(split));
        PageInfo<Movie> moviePageInfo = new PageInfo<>(movieList);
        return HigherResponse.getSuccessRespon(moviePageInfo);
    }


    //用户自己查询该电影的评价
    @RequestMapping("/getRating.do")
    public HigherResponse<Rating> getRating(HttpSession session,String movieId){
        Object attribute = session.getAttribute("userInfo");
        User user = null;
        if (attribute instanceof User){
            user = (User) attribute;
        }
        if (null == user){
            return HigherResponse.getFailedRespon("未登录，请重新登录");
        }
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("u_id",user.getUserId());
        wrapper.eq("m_id",movieId);
        Rating rating = ratingDao.selectOne(wrapper);
        if (null == rating){
            return HigherResponse.getFailedRespon("该用户没有对此电影做出评级");
        }
        return HigherResponse.getSuccessRespon(rating);
    }

    //用户 添加点评
    @RequestMapping("/addRating.do")
    public HigherResponse<String> addRating(HttpSession session,String movieId,String type, Integer rate){
        Object attribute = session.getAttribute("userInfo");
        User user = null;
        if (attribute instanceof User){
            user = (User) attribute;
        }
        if (null == user){
            return HigherResponse.getFailedRespon("未登录，请重新登录");
        }
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("u_id",user.getUserId());
        wrapper.eq("m_id",movieId);
        Rating rating1 = ratingDao.selectOne(wrapper);
        if (null != rating1){
            return HigherResponse.getFailedRespon("该用户已经做出评级");
        }
        Rating rating = new Rating();
        rating.setUserId(user.getUserId());
        rating.setMovieId(movieId);
        rating.setType(type);
        rating.setRate(rate);
        int insert = ratingDao.insert(rating);
        if (insert == 0){
            return HigherResponse.getFailedRespon("评价失败");
        }
        return HigherResponse.getSuccessRespon("评价成功");

    }

    //查询
    //25945280,25820460,25786060,25955779,1652587,1866473,25794302,1432146,6390823,1794171
    //4925418,2001296,25980249,1458898,1458899,1458889,26265989,1458894,1477924,20458867
    //根据电影id 查询rating表中的数据
    @RequestMapping("recommendMovies.do")
    public HigherResponse<Recommend> recommendMovies(HttpSession session,String movieId){
        Object attribute = session.getAttribute("userInfo");
        User user = null;
        if (attribute instanceof User){
            user = (User) attribute;
        }
        if (null == user){
            return HigherResponse.getFailedRespon("未登录，请重新登录");
        }
        Recommend recommend = recommendDao.selectById(movieId);
        return HigherResponse.getSuccessRespon(recommend);
    }


}
